/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package multiface.web.util;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.PixelGrabber;
import java.util.ArrayList;

public class ImageConverter extends Component {

    private MediaTracker mediaTracker;
    private Image image;

    private ImageConverter(Image image) {
        super();
        this.mediaTracker = new MediaTracker(this);
        this.mediaTracker.addImage(image, 0);
        this.image = image;
    }

    private BufferedImage convert() {
        /*
         * Have to wait for image to load.
         */
        try {
            this.mediaTracker.waitForID(0);
        } catch (InterruptedException e) {
        }
        //Sy_stem.out.println("-1");

        GraphicsConfiguration graphicsConfig = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
        BufferedImage bimage = graphicsConfig.createCompatibleImage(this.image.getWidth(null), this.image.getHeight(null));
        //Sy_stem.out.println("-2");
        Graphics g = bimage.getGraphics();
        g.drawImage(image, 0, 0, null);
        return bimage;
    }

    private static byte[] convertIntToByteArray(int integer) {
        byte[] bytes = new byte[4];
        bytes[0] = (byte) (integer >> 24);
        bytes[1] = (byte) ((integer << 8) >> 24);
        bytes[2] = (byte) ((integer << 16) >> 24);
        bytes[3] = (byte) ((integer << 24) >> 24);
        return bytes;
    }

    private static int convertByteArrayToInt(byte[] bytes) {
        return (bytes[0] << 32) | (bytes[1] << 24) | (bytes[2] << 16) | (bytes[3] << 8) | bytes[4];
    }

    private static byte[] convertIntArrayToByteArray(int[] integers) {
        byte[] bytes = new byte[integers.length * 4];
        for (int index = 0; index < integers.length; index++) {
            byte[] integerBytes = convertIntToByteArray(integers[index]);
            bytes[index * 4] = integerBytes[0];
            bytes[1 + (index * 4)] = integerBytes[1];
            bytes[2 + (index * 4)] = integerBytes[2];
            bytes[3 + (index * 4)] = integerBytes[3];
        }
        return bytes;
    }

    private static int[] convertByteArrayToIntArray(byte[] bytes) {
        ArrayList integers = new ArrayList();
        for (int index = 0; index < bytes.length; index += 4) {
            byte[] fourBytes = new byte[4];
            fourBytes[0] = bytes[index];
            fourBytes[1] = bytes[index + 1];
            fourBytes[2] = bytes[index + 2];
            fourBytes[3] = bytes[index + 3];
            int integer = convertByteArrayToInt(fourBytes);
            integers.add(new Integer(integer));
        }
        int[] ints = new int[bytes.length / 4];
        for (int index = 0; index < integers.size(); index++) {
            ints[index] = ((Integer) integers.get(index)).intValue();
        }
        return ints;
    }

    public static byte[] convertToBytes(Image image) {
        //Sy_stem.out.println("A");
        ImageConverter converter = new ImageConverter(image);
        //Sy_stem.out.println("B");
        BufferedImage bufferedImage = converter.convert();
        //Sy_stem.out.println("C");
        PixelGrabber pixelGrabber = new PixelGrabber(image, 0, 0, bufferedImage.getWidth(), bufferedImage.getHeight(), true);
        //Sy_stem.out.println("D");
        try {
            if (pixelGrabber.grabPixels()) {
                Object pixels = pixelGrabber.getPixels();
                if (pixels instanceof byte[]) {
                    return (byte[]) pixels;
                }
                return convertIntArrayToByteArray((int[]) pixels);
            }
        } catch (InterruptedException e) {
        }
        return null;
    }
}